package game.model;

import com.sghd.common.utils.json.JsonUtils;
import data.entity.GameRecord;
import data.entity.PK;
import data.model.BasePlayer;
import data.model.PropType;
import org.apache.commons.collections.MapUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author fank
 * @date 2018/03/08
 */
public class DeskPlayer extends BasePlayer {
    /** 剩余免费次数*/
    private int freeCount;
    /** 免费中赢取的金币*/
    private long freeMoney;
    /** 剩余未完成的小游戏*/
    private List<MGame> mgames;
    /** 上一轮压注*/
    private int betIndex;
    /** 上一轮连击次数*/
    private int combo;
    /** 购连击服务次数*/
    private int buyTimes = 0;

    public DeskPlayer(long playerId, String nickName) {
        super(playerId, nickName);
    }

    public DeskPlayer(long playerId, String nickName, String headUrl, int playerSex, String presentation, int vipLevel) {
        super(playerId, nickName, headUrl, playerSex, presentation, vipLevel);
    }

    public int getFreeCount() {
        return freeCount;
    }

    public void setFreeCount(int freeCount) {
        this.freeCount = freeCount;
    }

    public List<MGame> getMgames() {
        return mgames;
    }

    public void setMgames(List<MGame> mgames) {
        this.mgames = mgames;
    }

    public long getFreeMoney() {
        return freeMoney;
    }

    public void setFreeMoney(long freeMoney) {
        this.freeMoney = freeMoney;
    }

    /**
     * 还原记录
     * */
    public void setRecord(GameRecord record) {
        this.freeCount = record.getFreeCount();
        this.freeMoney = record.getFreeMoney();
        this.mgames = JsonUtils.string2Collection(record.getMgames(), ArrayList.class, MGame.class);
        this.betIndex = record.getBetIndex();
        this.combo = record.getCombo();
        this.buyTimes = record.getBuyTime();
    }

    public static void main(String[] args) {
        JsonUtils.string2Collection("[{\"sequen\":[4,0,6,0,6,3,6],\"scope\":5.0,\"position\":[1,10]}]", List.class, MGame.class);
    }

    /**
     * 获取记录
     * */
    public GameRecord getRecord(PK pk) {
        GameRecord record = new GameRecord(pk);
        record.setFreeCount(this.freeCount);
        record.setFreeMoney(this.freeMoney);
        record.setMgames(JsonUtils.object2String(mgames));
        record.setBetIndex(this.betIndex);
        record.setCombo(this.combo);
        record.setBuyTime(this.buyTimes);
        return record;
    }

    public int getBetIndex() {
        return betIndex;
    }

    public void setBetIndex(int betIndex) {
        this.betIndex = betIndex;
    }

    public void change(PropType type, long change) {
        long value = MapUtils.getLong(bagInfo, type);
        value += change;
//        if (value < 0) {
//            value = 0;
//        }
        bagInfo.put(type, value);
    }

    public int getCombo() {
        return combo;
    }

    public void setCombo(int combo) {
        this.combo = combo;
    }

    public int getBuyTimes() {
        return buyTimes;
    }

    public void setBuyTimes(int buyTimes) {
        this.buyTimes = buyTimes;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        DeskPlayer that = (DeskPlayer) o;
        return playerId == that.playerId;
    }

    @Override
    public int hashCode() {
        return Objects.hash(playerId);
    }
}
